﻿namespace YidanSoft.Library.EditorUtility.WinControls
{
    using System;
    using System.Collections;
    using System.Diagnostics;
    using System.Windows.Forms;

    public class CompareListItems : IComparer
    {
        private ListViewEx listView = null;

        public CompareListItems(ListViewEx lv)
        {
            this.listView = lv;
        }

        public int Compare(object obj1, object obj2)
        {
            ListViewItem item = (ListViewItem) obj1;
            ListViewItem item2 = (ListViewItem) obj2;
            RowSorterHelper rowSorterHelper = this.listView.GetRowSorterHelper();
            string text = item.Text;
            string str2 = item2.Text;
            int num = 0;
            if (this.listView.LastSortedColumn != 0)
            {
                text = item.SubItems[this.listView.LastSortedColumn].Text;
                str2 = item2.SubItems[this.listView.LastSortedColumn].Text;
                Debug.Assert((obj1 != null) && (obj2 != null));
            }
            if (rowSorterHelper != null)
            {
                if (rowSorterHelper.Format == SortedListViewFormatType.String)
                {
                    return this.CompareStrings(text, str2, this.listView.Sorting);
                }
                if (rowSorterHelper.Format == SortedListViewFormatType.Numeric)
                {
                    return this.CompareNumbers(text, str2, this.listView.Sorting);
                }
                if (rowSorterHelper.Format == SortedListViewFormatType.Date)
                {
                    return this.CompareDates(text, str2, this.listView.Sorting);
                }
                if (rowSorterHelper.Format == SortedListViewFormatType.Custom)
                {
                    if (rowSorterHelper.SortEvent == null)
                    {
                        return num;
                    }
                    num = rowSorterHelper.SortEvent((ListViewItem) obj1, (ListViewItem) obj2);
                    if (this.listView.Sorting == SortOrder.Descending)
                    {
                        num *= -1;
                    }
                }
                return num;
            }
            if (rowSorterHelper == null)
            {
                num = this.CompareStrings(text, str2, this.listView.Sorting);
            }
            return num;
        }

        private int CompareDates(string string1, string string2, SortOrder sortOrder)
        {
            DateTime time = DateTime.Parse(string1);
            DateTime time2 = DateTime.Parse(string2);
            int num = DateTime.Compare(time, time2);
            if (sortOrder == SortOrder.Descending)
            {
                num *= -1;
            }
            return num;
        }

        private int CompareNumbers(string string1, string string2, SortOrder sortOrder)
        {
            float num = float.Parse(string1);
            float num2 = float.Parse(string2);
            int num3 = num.CompareTo(num2);
            if (sortOrder == SortOrder.Descending)
            {
                num3 *= -1;
            }
            return num3;
        }

        private int CompareStrings(string string1, string string2, SortOrder sortOrder)
        {
            int num = string.Compare(string1, string2);
            if (sortOrder == SortOrder.Descending)
            {
                num *= -1;
            }
            return num;
        }
    }
}

